<?php
/**
 *
 * Created by PhpStorm.
 * User: Egor <http://gitee.com/seosite>
 * Date: 2018/5/4
 * Time: 下午1:52
 * Updatetime:
 * updater：
 */

namespace Wap\Controller;

use lyf\Tree;

class PublicController extends BaseController
{

    /**
     * @desc 点赞
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function addCommentParise($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,type');

        unset($check);
        //type 1 为评论， 2为提问
        if ($param['type']==1){
            $model = M('comment_parise');

            $comment_id = $param['id'];

            $is_exist= $model->where(['comment_id'=>$comment_id,'uid'=>$this->userInfo->id])->find();
            if (!$is_exist){
                $add_data = array(
                    'uid'           =>  $this->userInfo->id,
                    'comment_id'    =>  $comment_id,
                    'create_time'   =>  time(),
                );

                $operation = $model->add($add_data);

                if ($operation) {
                    M('comment')->where(['id'=>$comment_id])->setInc('praise_count');
                    return showErrorMsg(L('_OPERATION_SUCCESS_'));
                }else{
                    return showErrorMsg(L('_OPERATION_FAIL_'),100002);
                }
            }
            return showErrorMsg('你已经点赞过啦~',100002);
        }elseif ($param['type']==2){
            $model = M('qa_praise');

            $comment_id = $param['id'];

            $is_exist= $model->where(['qa_id'=>$comment_id,'uid'=>$this->userInfo->id])->find();
            if (!$is_exist){
                $add_data = array(
                    'uid'           =>  $this->userInfo->id,
                    'qa_id'    =>  $comment_id,
                    'create_time'   =>  time(),
                );

                $operation = $model->add($add_data);

                if ($operation) {
                    M('qa')->where(['id'=>$comment_id])->setInc('praise_count');
                    return showErrorMsg(L('_OPERATION_SUCCESS_'));
                }else{
                    return showErrorMsg(L('_OPERATION_FAIL_'),100002);
                }
            }
            return showErrorMsg('你已经点赞过啦~',100002);
        }

    }

    /**
     * @desc 取消赞
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function removeCommentParise($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,type');

        unset($check);

//type 1：评论  ，2，提问
        if ($param['type']==1){
            $model = M('comment_parise');

            $comment_id = $param['id'];

            $map['comment_id'] = $comment_id;
            $map['uid'] = $this->userInfo->id;
            $is_exist = $model->where($map)->find();
            if ($is_exist){
                $operation = $model->where($map)->delete();

                if ($operation) {
                    M('comment')->where(['id'=>$comment_id])->setDec('praise_count');
                    return showErrorMsg(L('_OPERATION_SUCCESS_'));
                }else{
                    return showErrorMsg(L('_OPERATION_FAIL_'),100002);
                }

            }else{
                return showErrorMsg(L('_OPERATION_SUCCESS_'));
            }
        }elseif ($param['type']==2){
            $model = M('qa_praise');

            $comment_id = $param['id'];

            $map['qa_id'] = $comment_id;
            $map['uid'] = $this->userInfo->id;
            $is_exist = $model->where($map)->find();
            if ($is_exist){
                $operation = $model->where($map)->delete();

                if ($operation) {
                    M('qa')->where(['id'=>$comment_id])->setDec('praise_count');
                    return showErrorMsg(L('_OPERATION_SUCCESS_'));
                }else{
                    return showErrorMsg(L('_OPERATION_FAIL_'),100002);
                }

            }else{
                return showErrorMsg(L('_OPERATION_SUCCESS_'));
            }
        }

    }

    /**
     * @desc 添加收藏
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function addCollect($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,type');

        unset($check);

        $model = M('collect');
        $map['type']    = $param['type'];
        $map['work_id'] = $param['id'];
        $map['uid'] = $this->userInfo->id;
        $info = $model->where($map)->find();
        if (empty($info)){
            $add_data = array(
                'work_id'   =>  $param['id'],
                'type'   =>  $param['type'],
                'uid'   =>  $this->userInfo->id,
                'create_time'   =>  time(),
            );

            $operation = $model->add($add_data);
            if ($operation) {

                $total_count = M('collect')->where(['type'=>$param['type'],'work_id'=>$param['id']])->count();

                if ($total_count==50){

                    //收藏的业务类型：1 视频，2视频直播，3音频直播，4音频，5报告，6图片，7快讯，8数据，9会议，10专栏，11问答，12问卷
                    switch ($param['type']){
                        case 1:
                            $model = M('video');
                            $info = $model->where(['id'=>$param['id']])->find();
                            $uid = $info['uid'];
                            $title = $info['title'];


                            break;
                        case 2:
                            $model = M('direct');
                            break;
                        case 3:
                            $model = M('audio');
                            break;
                        case 4:
                            $model = M('mp3');
                            break;
                        case 5:
                            $model = M('report');
                            break;
                        case 6:
                            $model = M('picture');
                            break;
                        case 7:
                            $model = M('news');
                            break;
                        case 8:
                            $model = M('data');
                            break;
                        case 9:
                            $model = M('meeting');
                            break;
                        case 10:
                            $model = M('columnist');
                            break;
                        case 11:
                            $model = M('question');
                            break;
                        case 12:
                            $model = M('qa');
                            break;
                    }


                    $uid = $model->where(['id'=>$param['id']])->getField('uid');

                    message_record($uid,'13','模块续费成功','你于'.date('Y-m-d H:i:s',$orderInfo['create_time']).'成功续费了「'.$v['title'].'」模块，有效期截止至'.date('Y/m/d H:i:s',$add_time).'，感谢您的支持。');
                }



                return showErrorMsg(L('_OPERATION_SUCCESS_'));
            }
            return showErrorMsg(L('_OPERATION_FAIL_'),100002);
        }else{
            return showErrorMsg('已经收藏过啦！',100002);
        }

    }

    /**
     * @desc 取消收藏
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function removeCollect($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,type');

        unset($check);

        $model = M('collect');
        $map['type']    = $param['type'];
        $map['work_id'] = $param['id'];
        $info = $model->where($map)->find();
        if ($info){
            $add_data = array(
                'work_id'   =>  $param['id'],
                'type'   =>  $param['type'],
                'uid'   =>  $this->userInfo->id,
                'create_time'   =>  time(),
            );

            $operation = $model->where($map)->delete();
            if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
            return showErrorMsg(L('_OPERATION_FAIL_'),100002);
        }else{
            return showErrorMsg(L('_OPERATION_SUCCESS_'));
        }

    }

    /**
     * @desc 添加评论
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function addComment($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,type,puid,content');

        unset($check);


        $model = M('comment');

        //过滤评论内容
        vendor('Efilter.Efilter');
        $efiter = new \Efilter($param['content'],C('mmp'));
        $content = $efiter->filter();

        $add_data = array(
            'work_id'   =>  $param['id'],
            'type'      =>  $param['type'],
            'uid'       =>  $this->userInfo->id,
            'puid'      =>  empty($param['puid'])?0:$param['puid'],
            'content'   =>  $content,
            'create_time'   =>  time(),
        );

        $operation = $model->add($add_data);
        if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
        return showErrorMsg(L('_OPERATION_FAIL_'),100002);
    }

    /**
     * @desc 删除评论
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function delComment($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        M('comment')->where(['id'=>$param['id']])->delete();
        M('comment')->where(['puid'=>$param['id']])->delete();

        return showErrorMsg(L("_OPERATION_SUCCESS_"));
    }

    /**
     * @desc 添加爱心关注
     * @author Earl<guo@seosite.cn>
     * @param $param
     * @return array
     */
    public function addConnect($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,type');

        unset($check);

        $model = M('connect');
        $map['type']    = $param['type'];
        $map['work_id'] = $param['id'];
        $map['uid'] = $this->userInfo->id;
        $info = $model->where($map)->find();

        if (!$info){
            $add_data = array(
                'work_id'   =>  $param['id'],
                'type'   =>  $param['type'],
                'uid'   =>  $this->userInfo->id,
                'create_time'   =>  time(),
            );

            $operation = $model->add($add_data);
            if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
            return showErrorMsg(L('_OPERATION_FAIL_'),100002);
        }else{
            return showErrorMsg('已经关注过啦！',100002);
        }
    }

    /**
     * @desc 取消爱心关注
     * @author Earl<guo@seosite.cn>
     * @param $param
     * @return array
     */
    public function removeConnect($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,type');

        unset($check);

        $model = M('connect');
        $map['type']    = $param['type'];
        $map['work_id'] = $param['id'];
        $map['uid'] = $this->userInfo->id;
        $info = $model->where($map)->find();

        if ($info){
            $operation = $model->where($map)->delete();
            if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
            return showErrorMsg(L('_OPERATION_FAIL_'),100002);
        }else{
            return showErrorMsg(L('_OPERATION_SUCCESS_'));
        }
    }


    /**
     * @desc 添加关注的人
     * @author Earl<guo@seosite.cn>
     * @param $param
     * @return array
     */
    public function addFollow($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('user_follow');
        $map['uid'] = $this->userInfo->id;
        $map['puid'] = $param['id'];

        if ($this->userInfo->id == $param['id']) return showErrorMsg('你不可以关注自己！',100002);

        $info = $model->where($map)->find();

        if (!$info){
            $add_data = array(
                'puid'          =>  $param['id'],
                'uid'           =>  $this->userInfo->id,
                'rank'          =>  '',
                'status'        =>  1,
                'create_time'   =>  time(),
            );

            $operation = $model->add($add_data);
            if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
            return showErrorMsg(L('_OPERATION_FAIL_'),100002);
        }else{
            return showErrorMsg('已经关注过啦！',100002);
        }
    }

    /**
     * @desc 取消关注
     * @author Earl<guo@seosite.cn>
     * @param $param
     * @return array
     */
    public function removeFollow($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('user_follow');
        $map['puid']    = $param['id'];
        $map['uid'] = $this->userInfo->id;
        $info = $model->where($map)->find();

        if ($info){
            $operation = $model->where($map)->delete();
            if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
            return showErrorMsg(L('_OPERATION_FAIL_'),100002);
        }else{
            return showErrorMsg(L('_OPERATION_SUCCESS_'));
        }
    }

    /**
     * @desc 添加推荐
     * @author Earl<guo@seosite.cn>
     * @param $param
     * @return array
     */
    public function addRecommend($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,type');

        unset($check);

        $model = M('recommend');
        $map['type']    = $param['type'];
        $map['work_id'] = $param['id'];
        $map['uid'] = $this->userInfo->id;
        $info = $model->where($map)->find();

        if (!$info){

            $add_data = array(
                'work_id'   =>  $param['id'],
                'type'      =>  $param['type'],
                'uid'       =>  $this->userInfo->id,
                'create_time'   =>  time(),

            );

            $operation = $model->add($add_data);
            if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
            return showErrorMsg(L('_OPERATION_FAIL_'),100002);
        }else{
            return showErrorMsg("已经被推荐过啦!",100002);
        }
    }

    /**
     * @desc 用户标签列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function userTagList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('type');

        unset($check);

        $model = M('tag');

        $where['uid'] = array('in','0,'.$this->userInfo->id);

        $where['type'] = $param['type'];

        //标签分类：1 视频，2直播，3音频，4报告，5图片，6快讯，7数据，8会议，9专栏，10问答，11问卷,12 用户关注范围,13 音频直播

        list($list,$page) = _listPage($model,$where,1,'num asc,id desc','id,title',100);

        return showList($list,$page);
    }

    /**
     * @desc 标签列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function tagList($param)
    {
        $check = CheckFactoryController::isChildParamSet();
        $check->before('type');

        unset($check);

        $model = M('tag');

        $where['uid'] = 0;

        $where['type'] = $param['type'];

        //标签分类：1 视频，2直播，3音频，4报告，5图片，6快讯，7数据，8会议，9专栏，10问答，11问卷,12 用户关注范围,13 音频直播

        list($list,$page) = _listPage($model,$where,1,'num asc,id desc','id,title',100);

        return showList($list,$page);
    }


    /**
     * @desc 提问的用户列表，按照字母排序
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function questionUsers($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('User');
        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $keyword = $param['keyword'];

        $where['status'] = 1;

        if(!empty($keyword)){
            $where['nickname'] = array('like','%'.$keyword.'%');
        }

        list($listRet, $page) = _listPage($model, $where, $p, 'first_name asc', "group_concat(id) ids,first_name ",$listRows ,'first_name');

        if (!empty($listRet)){
            foreach ($listRet as $k=>$v){
                if (empty($v['first_name'])){
//                    $v['first_name'] = '其他';
                    unset($listRet[$k]);
                }else{
                    $idArr = explode(',',$v['ids']);
                    foreach ($idArr as $key=>$val){
                        $listRet[$k]['_child'][$key] = $model->field('id,u_no,nickname,headsmall,j_name')->find($val);
                        $listRet[$k]['_child'][$key]['fee'] = M('user_auth')->where(['uid'=>$val,'title'=>'qa'])->getField('fee');
                    }
                    unset($listRet[$k]['ids']);
                }

            }
        }
        return showList(array_merge($listRet),$page);
    }

    /**
     * @desc 提问
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function addQuestion($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('content,type,id,uid,content_type');

        unset($check);

        $uids = explode(',',$param['uid']);

        $temp_arr = array();
        $temp_fee = array();
        foreach ($uids as &$v){
            $info = M('user')->field('id,u_no')->where(['id'=>$v])->find();

            if (!empty($info)){
                $temp_arr[] = $info['u_no'];
            }

            $is_exist = M('user_auth')->where(['uid'=>$v])->find();
            if ($is_exist){
                $temp_fee[] = $is_exist['fee'];
            }
        }

        $u_no = implode(',',$temp_arr);

        $fee = array_sum($temp_fee);

        $model = M('qa');
        $insert_data = array(
            'uid'               =>  $this->userInfo->id,
            'content'           =>  $param['content'],
            'tag_id'            =>  '',
            'status'            =>  1,
            'views'             =>  0,
            'update_time'       =>  '',
            'create_time'       =>  time(),
            'is_show'           =>  1,
            'ans_uid'           =>  empty($u_no)?'':$u_no,
            'work_id'           =>  $param['id'],
            'type'              =>  $param['type'],
            'content_type'      =>  $param['content_type'],
            'fee'               =>  empty($fee)?'0.00':$fee,
            'is_pay'            =>  0
        );

        $operation = $model->add($insert_data);
        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,array('id'=>$operation));
        return showErrorMsg(L("_OPERATION_FAIL_",100002));
    }

    /**
     * @desc 评论回复列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function commentReply($param)
    {
        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('comment');
        $map['pid'] = $param['id'];

        list($commentList,$commentPage) = _listPage($model,$map,1,'create_time desc','id,uid,type,content,praise_count,create_time',$listRows1);
        if (!empty($commentList)){
            foreach ($commentList as $k=>$v){
                $info = M('user')->where(['id'=>$v['uid']])->find();
                $commentList[$k]['u_name'] = $info['nickname'];
                $commentList[$k]['u_headsmall'] = $info['headsmall'];
                $commentList[$k]['child_count'] = M('comment')->where(['pid'=>$v['id']])->count();
            }
        }
        return showList($commentList,$commentPage);
    }

    /**
     * @desc 评论列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function commentList($param)
    {
        $check = CheckFactoryController::isChildParamSet();
        $check->before('type,id');

        unset($check);

        //评论
        $p1 = empty($param['p1']) ?: $param['p1'];
        $listRows1 = empty($param['listRows1']) ? 20 : $param['listRows1'];

        $comment_map['work_id'] = $param['id'];
        $comment_map['type'] = $param['type'];
        $comment_map['pid']  = 0;
        list($commentList,$commentPage) = _listPage(M('comment'),$comment_map,$p1,'create_time desc','id,uid,type,content,praise_count,create_time',$listRows1);
        if (!empty($commentList)){
            foreach ($commentList as $k=>$v){
                $info = M('user')->where(['id'=>$v['uid']])->find();
                $commentList[$k]['u_name'] = $info['nickname'];
                $commentList[$k]['u_headsmall'] = $info['headsmall'];
                $commentList[$k]['child_count'] = M('comment')->where(['pid'=>$v['id']])->count();
            }
        }

        return showList($commentList,$commentPage);

    }

    /**
     * @desc 区域地址
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function areaList()
    {
        //国内省市1526澳门

        $model = M('loclist');

        $map['id'] = array('elt',1526);


        $list = $model->field('id,name,parent_id')->where($map)->select();
        $tree = new Tree();
        $a = $tree->list2tree($list,'id','parent_id','_child','0');

        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$a[0]['_child']);
    }

    /**
     * @desc
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function giftList()
    {
        $model = M('gifts');

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $map['status'] = 1;

        list($list,$page) = _listPage($model,$map,$p,'create_time desc','id,title,cover_img,price',$listRows);

        return showList($list,$page);
    }
}